﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Storage.Streams;

namespace MyHelloWinDevice
{
    public class CDeviceManger
    {
        public event TypedEventHandler<CDeviceManger, DeviceContext> Added;
        public event TypedEventHandler<CDeviceManger, object> Completed;
        public event TypedEventHandler<CDeviceManger, DeviceContext> Removed;
        public event TypedEventHandler<CDeviceManger, object> Stopped;
        public event TypedEventHandler<CDeviceManger, DeviceContext> Updated;
        
        Bluetooth m_Bluetooth = new Bluetooth();
        VirtualDevices m_Virtual = new VirtualDevices();

        public Boolean StartMonitor()
        {
            if(!m_Bluetooth.RegistMonitor(this)){
                StopMonitor();
                return false;
            }

            if (!m_Virtual.StartMonitor(this))
            {
                StopMonitor();
                return false;
            }

            return true;
        }

        public void StopMonitor()
        {
            m_Bluetooth.StopMonitor();
            m_Virtual.StopMonitor();
        }

        public Boolean RegistMonitorWithTask(string strTaskPreName, string strEntryPoint)
        {
            if (!m_Bluetooth.RegistMonitor(this, strTaskPreName, strEntryPoint))
            {
                StopMonitor();
                return false;
            }

            return true;
        }

        public Boolean ClearMonitorWithTask(string strTaskPreName)
        {
            if (!m_Bluetooth.ClearMonitor(strTaskPreName))
            {
                return false;
            }

            return true;
        }

        public void DealwithDevicesWithTask(string strTaskName, IBackgroundTaskInstance taskInstance)
        {
            if (strTaskName.Contains(Bluetooth.g_strTaskPostName))
            {
                m_Bluetooth.DealWithMonitor(this, taskInstance);
            }
        }

        public int VeiwAllDevicesWithTask()
        {
            int rv = 1;
            DeviceContext Context = new DeviceContext();
            Context.Type = DeviceType.Virtual;
            Context.DeviceID = "7DD474A8-1CBE-4A67-943C-7ABBD2C2DE78";
            Context.DeviceName = "MyHelloWinVirtualDevice";
            Context.IsConnected = true;
            AddEvent(Context);

            rv += m_Bluetooth.VeiwAllDevicesWithTask();

            return rv;
        }

        public Boolean GetKey(DeviceContext Driver, out IBuffer deviceKey, out IBuffer authKey)
        {
            deviceKey = null;
            authKey = null;

            return false;
        }
        
        public void AddEvent(DeviceContext Context)
        {
            if(Added != null)
            {
                Added(this, Context);
            }
        }

        public void RemoveEvent(DeviceContext Context)
        {
            if(Removed != null)
            {
                Removed(this, Context);
            }
        }

        public void StopEvent(DeviceContext Context)
        {
            if(Stopped != null)
            {
                Stopped(this, Context);
            }
        }

        public void UpdateEvent(DeviceContext Context)
        {
            if (Updated != null) { 
                Updated(this, Context);
            }
        }

        public void CompleteEvent(DeviceContext Context)
        {
            if(Completed != null)
            {
                Completed(this, Context);
            }
        }
    }
}
